<?php

/**
 * 推荐位管理model操作
 * @author  xiaobai
 * @date    2018-01-28
 */

namespace app\common\model;

use app\common\model\Common;

class Position extends Common
{
    const STATUS_OPEN  = 1;     // 开启
    const STATUS_CLOSE = 0;     // 关闭

    protected $pk   = 'id';
    protected $auto = ['admin_id'];
    protected $autoWriteTimestamp = 'datetime'; // 自动写入时间，格式为Y-m-d H:i:s

    /**
     * 操作人id自动完成
     * @return mixed
     */
    protected function setAdminIdAttr()
    {
        return get_admin_id();
    }

    /**
     * 获取状态信息
     * @return array
     */
    public function getStatus()
    {
        $status = array(
            self::STATUS_OPEN  => '开启',
            self::STATUS_CLOSE => '关闭',
        );
        return $status;
    }

    /**
     * 获取推荐位列表
     * @param array $where  查询where条件
     * @return bool|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getList($where = array('status'=>self::STATUS_OPEN))
    {
        $key = 'pos_list_'.md5(json_encode($where));
        $pos_list = cache($key);

        if (empty($pos_list)) {
            $pos_list = $this->where($where)->column('id,name');
            if (!empty($pos_list)) {
                cache($key, $pos_list);
            }
        }

        return $pos_list;
    }
}
